//
//  SecondCollectionViewCell.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/23.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SecondCollectionViewCell.h"
#import "CustomerNetworking.h"
#import "SecondModel.h"
#import "SecondTableViewCell.h"

@interface SecondCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, assign) CGFloat contenfOffSetY;
@end
@implementation SecondCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        [self createSecond];
    }
    return self;
}

- (void)createSecond
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 677) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = [UIColor clearColor];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 250)];
    headView.alpha = 0;
    self.tableview.tableHeaderView = headView;
    self.contenfOffSetY = _tableview.contentOffset.y;
    UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableview.tableFooterView = footView;
    [self.tableview registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"secondTableViewCell"];
    [self.contentView addSubview:_tableview];
}

- (void)customer
{
    NSString *string = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/albums?page=2&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88243&longtitude=121.54", _IDString];
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            SecondModel *second = [SecondModel secondModelWithDictionary:dic];
            [self.array addObject:second];
        }
        [self.tableview reloadData];
    }];
}

- (void)setIDString:(NSString *)IDString
{
    if (_IDString != IDString) {
        [_IDString release];
        _IDString = [IDString copy];
        [self customer];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondTableViewCell" forIndexPath:indexPath];
    cell.secondModel = [self.array objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y < _contenfOffSetY) {
//        [self.delegate SecondTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:NO];
//    }else {
//        [self.delegate SecondTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:YES];
//    }
//    _contenfOffSetY = scrollView.contentOffset.y;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushSecondController:[[self.array objectAtIndex:indexPath.row] albumId]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
